﻿using LD.Controls.ControlHelper;
using ScottPlot;
using System;
using System.Collections.Generic;
using System.Linq;

namespace LD
{
    public partial class FrmMachineInfo3 : FrmBase
    {
        public FrmMachineInfo3()
        {
            InitializeComponent();
            Init();
            FormScaler.ScaleForm(this, true);//自动缩放
        }
        /// <summary>
        /// 曲线更新事件图标实现方法
        /// </summary>
        /// <param name="name">PlcCommManger配置的名字</param>
        /// <param name="screwData">单个螺丝数据</param>
        /// <param name="data">螺丝的序号，扭矩和角度</param>
        private void Instance_ReceiveDataEvent(string name, TB_Screw screwData, List<ScrewDataModel> data)
        {
            if (!this.IsDisposed)
            {
                this.Invoke(new Action(() =>
                {
                    ParseData(name, screwData, data);
                }));
            }
        }

        private void Init()
        {
            this.formsPlot1.Configuration.ScrollWheelZoom = formsPlot2.Configuration.ScrollWheelZoom = formsPlot3.Configuration.ScrollWheelZoom = false;
            this.formsPlot1.Configuration.LockHorizontalAxis = formsPlot2.Configuration.LockHorizontalAxis = formsPlot3.Configuration.LockHorizontalAxis = false;
            this.formsPlot1.Configuration.LockVerticalAxis = formsPlot2.Configuration.LockVerticalAxis = formsPlot3.Configuration.LockVerticalAxis = false;

            //var leftPlot =  formsPlot1.Plot;
            //var rightPlot = formsPlot2.Plot;

            formsPlot1.Plot.Title(PartNoHelper.Instance.CurrentPartNo.Modules[0].Name);
            formsPlot1.Plot.XLabel("角度");
            formsPlot1.Plot.YLabel("扭矩");

            formsPlot2.Plot.Title(PartNoHelper.Instance.CurrentPartNo.Modules[1].Name);
            formsPlot2.Plot.XLabel("角度");
            formsPlot2.Plot.YLabel("扭矩");

            formsPlot3.Plot.Title(PartNoHelper.Instance.CurrentPartNo.Modules[2].Name);
            formsPlot3.Plot.XLabel("角度");
            formsPlot3.Plot.YLabel("扭矩");

            formsPlot1.Plot.AddHorizontalLine(double.Parse(PartNoHelper.Instance.CurrentPartNo.Modules[0].Parame["MaxTorque"]));
            formsPlot1.Plot.AddHorizontalLine(double.Parse(PartNoHelper.Instance.CurrentPartNo.Modules[0].Parame["MinTorque"]));

            formsPlot2.Plot.AddHorizontalLine(double.Parse(PartNoHelper.Instance.CurrentPartNo.Modules[1].Parame["MaxTorque"]));
            formsPlot2.Plot.AddHorizontalLine(double.Parse(PartNoHelper.Instance.CurrentPartNo.Modules[1].Parame["MinTorque"]));

            formsPlot3.Plot.AddHorizontalLine(double.Parse(PartNoHelper.Instance.CurrentPartNo.Modules[2].Parame["MaxTorque"]));
            formsPlot3.Plot.AddHorizontalLine(double.Parse(PartNoHelper.Instance.CurrentPartNo.Modules[2].Parame["MinTorque"]));


            formsPlot1.Refresh();
            formsPlot2.Refresh();
            formsPlot3.Refresh();

            foreach (var item in ScrewManger.Instance.ScrewHelpers)
            {
                item.Value.ReceiveDataEvent += Instance_ReceiveDataEvent;
            }
        }

        private void ParseData(string name, TB_Screw screwData, List<ScrewDataModel> data)
        {
            Plot mPlot = new Plot();
            var screwInfo = PartNoHelper.Instance.CurrentPartNo.Modules.Where(p => p.Name == name).FirstOrDefault();
            if (screwInfo == null)
                return;

            if (screwInfo.Index == 1)//第一个就是曲线图1
            {
                mPlot = formsPlot1.Plot;
                mPlot.Clear();
                formsPlot1.Refresh();
            }
            else if (screwInfo.Index == 2) //曲线图2
            {
                mPlot = formsPlot2.Plot;
                mPlot.Clear();
                formsPlot2.Refresh();
            }
            else if (screwInfo.Index == 3)//曲线图3
            {

                mPlot = formsPlot3.Plot;
                mPlot.Clear();
                formsPlot3.Refresh();
            }
            else
            {
                return;
            }

            double[] x = new double[data.Count];
            double[] y = new double[data.Count];
            for (int i = 0; i < data.Count; i++)
            {
                x[i] = data[i].角度;
                y[i] = data[i].扭矩;
            }
            if (x.Length > 0 && y.Length > 0)
            {

                mPlot.AddScatterLines(x, y);
                mPlot.AddHorizontalLine(double.Parse(screwInfo.Parame["MinTorque"]));
                mPlot.AddHorizontalLine(double.Parse(screwInfo.Parame["MaxTorque"]));
                mPlot.Title($"{screwInfo.Name}_曲线");

            }
            var bitmap = mPlot.Render();
            bitmap.Save(screwData.FilePath);
        }
    }
}
